package com.example.maze;

import tw.fc.agui.*;
import android.graphics.*;

class Cell {
   //[ ========== static part ==========
   final static int ENTRY    = 0,  // type
                    EXIT     = 1,
                    NORMAL   = 3,
                    MARK     = 5;
                    
   final static int NON_FOCUS= 0, // state
                    FOCUS    = 1;              
   final static int NO = 0,       // mark, trace
                    YES= 1;    
                   
   final static int NO_VISIT= 0,       // robotFootmark
                    VISITED = 1;                                                   
   
   final static int WEST = 0,     // direction
                    SOUTH= 1,
                    EAST = 2,
                    NORTH= 3;                                  
   //] ========== static part ==========
   //[ [0]=> west, [1]=> south, [2]=> east, [3]=> north
   boolean[] walls  = { true,  true,  true,  true};
   boolean[] borders= {false, false, false, false};
   //]
   int     posM, posN;  // Spanning tree�����I����m
   int     orgX, orgY;  // ø�Ϫ��_�l��m
   int     size;
   int     type          = NORMAL;
   int     state         = NON_FOCUS;
   int     mark          = NO;
   int     flag          = NO;
   int     trace         = NO;
//   int     robotFootmark = NO_VISIT; //>>>>還會用到嗎?
//   boolean isVisited= false;         //>>>>還會用到嗎?
//   int     distance;                 //>>>>還會用到嗎?
   int     colorValue;
   
   public Cell(int m, int n) {
      posM= m;
      posN= n;
   }
   public Cell(int m, int n, int s) {
      posM= m;
      posN= n;
      size= s;
      orgX= n*s; 
      orgY= m*s; 
   }
   public void setDrawOrg(int curM, int curN) {
      orgX= curN*size; orgY= curM*size;
   }
   
   //[ 畫牆
   private void paintWalls(GrCanvas g) {
      g.setAntiAlias(false); // 不畫斜線用不到
      int oldColor= g.getColor();
      g.setColor(Color.BLACK);
      if(walls[WEST])  g.drawLine(        orgX+1,       orgY+1,        orgX+1, orgY+size-1);
      if(walls[SOUTH]) g.drawLine(        orgX+1,  orgY+size-1,   orgX+size-1, orgY+size-1);
      if(walls[EAST])  g.drawLine(   orgX+size-1,       orgY+1,   orgX+size-1, orgY+size-1);
      if(walls[NORTH]) g.drawLine(        orgX+1,       orgY+1,   orgX+size-1,      orgY+1);     
      g.setColor(oldColor);
      g.setAntiAlias(true);
   }
   //[ 畫邊界
   private void paintBorders(GrCanvas g) {
      g.setAntiAlias(false);
      int oldColor= g.getColor();
      g.setColor(Color.GRAY);
      if(borders[WEST])  g.drawLine(    orgX,       orgY,      orgX, orgY+size);
      if(borders[SOUTH]) g.drawLine(    orgX,  orgY+size, orgX+size, orgY+size);
      if(borders[EAST])  g.drawLine(orgX+size, orgY+size, orgX+size,      orgY);
      if(borders[NORTH]) g.drawLine(orgX+size,      orgY,      orgX,      orgY);
      g.setColor(oldColor);
      g.setAntiAlias(true);
   }
   //[ 畫這種標記
   private void drawMark(GrCanvas g, int c) {
      int oldColor= g.getColor();
      g.setColor(c);
      if(type==ENTRY || type==EXIT || state==FOCUS) {
         g.fillOval(orgX+size/4, orgY+size/4, size/2, size/2);
      } else if( mark==YES ){ // 畫X
         g.drawLine(orgX+size/4, orgY+size/4, orgX+size/4*3, orgY+size/4*3);
         g.drawLine(orgX+size/4*3, orgY+size/4, orgX+size/4, orgY+size/4*3);
      } else {     
         g.drawOval(orgX+size/4, orgY+size/4, size/2, size/2);         
      }
      g.setColor(oldColor);
   }  
   private void drawFlag(GrCanvas g) {
      int oldColor= g.getColor();
      g.setColor(Color.BLACK);
      g.drawLine(orgX+size/2,orgY, orgX+size/2, orgY+size);
      g.setColor(Color.BLUE);
      g.fillRect(orgX+size/2,orgY, size/4, size/4);
      g.setColor(Color.WHITE);
      g.fillOval(orgX+size/15*11,orgY+size/15, size/8, size/8);
      g.setColor(Color.RED);
      g.fillRect(orgX+size/4*3,orgY, size/3, size/4);
      g.fillRect(orgX+size/2,orgY+size/4, size/12*8, size/4);
      g.setColor(oldColor);
   }
   public void paintTo(GrCanvas g) {
         paintBorders(g);
         paintWalls(g);
         
         if(trace==YES) {
            drawMark(g, Color.GRAY);
         }
         if(mark==YES) {
            drawMark(g, Color.MAGENTA);  
         }
         if(type==ENTRY) {
            drawMark(g, Color.BLUE);
         }
         if(type==EXIT) {
            drawMark(g, Color.RED);
         } 
         if(state==FOCUS) {
            drawMark(g, Color.GRAY);
         }
         if(flag==YES) {
            drawFlag(g);
         }
                  
   }
   
   @Override
   public String toString() {
      return "("+posM+","+posN+")";
   }
}